package com.cu.jiekong.service;

import com.alibaba.fastjson.JSON;
import com.cu.jiekong.common.bizconstants.BorrowStatus;
import com.cu.jiekong.constants.ResultStatus;
import com.cu.jiekong.dao.BorrowInfoMapper;
import com.cu.jiekong.frame.model.Ret;
import com.cu.jiekong.model.BorrowInfo;
import com.cu.jiekong.util.DateUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author jintian
 * @date 11:31
 */
@Service
public class BorrowInfoService extends BaseService {

    @Autowired
    private AmqpTemplate amqpTemplate;

    @Autowired
    private BorrowInfoMapper borrowInfoMapper;


    /**
     * 申请借款
     *
     * @return
     * @Param borrowInfo
     */
    public Ret applyBorrow(BorrowInfo borrowInfo, String sessionId) {
        // TODO 没redis服务器，暂时隐藏
        // 判断用户是否登录
       /* if (!checkLogin(sessionId)) {
            return new Ret(ResultStatus.RESULT_100000.getCode(), ResultStatus.RESULT_100000.getMsg());
        }*/
        // 设置状态为
        borrowInfo.setStatus("0");
        borrowInfo.setApplyTime(DateUtil.currentLongTime());
        // 将借款信息存入本地数据库中
        borrowInfoMapper.insert(borrowInfo);
        // 推送借款信息到风控系统
        amqpTemplate.convertAndSend("borrowInfo", JSON.toJSONString(borrowInfo));
        return new Ret(ResultStatus.RESULT_000000.getCode(), ResultStatus.RESULT_000000.getMsg());
    }


    /**
     * 根据借款单号查借款信息
     *
     * @param borrowId
     * @return
     */
    public BorrowInfo queryBorrowByBorrowId(int borrowId) {
        return borrowInfoMapper.selectByPrimaryKey(borrowId);
    }

    /**
     * 更改借款订单状态
     *
     * @param status
     * @param borrowId
     * @return
     */
    public boolean updateBorrowStatus(String status, int borrowId) {
        return borrowInfoMapper.updateBorrowStatus(status, borrowId) > 0;
    }

    /**
     * 更改借款审核信息
     *
     * @param borrowId  借款单号
     * @param isSuccess 借款审核是否通过
     * @param msg       审核信息
     * @return
     */
    public boolean updateAuditorInfo(int borrowId, boolean isSuccess, String msg) {
        String status = "";
        if (isSuccess) {
            status = BorrowStatus.auditor_success;
        } else {
            status = BorrowStatus.auditor_fail;
        }
        return borrowInfoMapper.updateAuditorInfo(borrowId, status, msg) > 0;
    }

    /**
     * 借款打款信息
     *
     * @param borrowId  借款单号
     * @param isSuccess 是否成功
     * @param msg       借款打款信息
     * @return
     */
    public boolean updateRemitInfo(int borrowId, boolean isSuccess, String msg) {
        String status = "";
        if (isSuccess) {
            status = BorrowStatus.remit_success;
        } else {
            status = BorrowStatus.remit_fail;
        }
        return borrowInfoMapper.updateRemitInfo(borrowId, status, msg) > 0;
    }

    /**
     * 判断订单是否审核过
     *
     * @param borrowId
     * @return
     */
    public boolean hasAuditor(int borrowId) {
        BorrowInfo borrowInfo = this.queryBorrowByBorrowId(borrowId);
        return StringUtils.isNotBlank(borrowInfo.getAuditorTime());
    }

}
